﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PhysicsSimulator
{
    class WayPoint : CGameObject
    {
        bool _active = false;

        public bool Active
        {
            get { return _active; }
            set { _active = value; }
        }
        float _h;

        public float H
        {
            get { return _h; }
            set { _h = value; }
        }
        float _l;

        public float L
        {
            get { return _l; }
            set { _l = value; }
        }
        float _vx;

        public float Vx
        {
            get { return _vx; }
            set { _vx = value; }
        }
        float _vy;

        public float Vy
        {
            get { return _vy; }
            set { _vy = value; }
        }
        float _t;

        public float T
        {
            get { return _t; }
            set { _t = value; }
        }

        CVectorForce _vt;
        CVectorForce _vtx;
        CVectorForce _vty;

        Texture2D _pointTexture;

        public Texture2D PointTexture
        {
            get { return _pointTexture; }
            set { _pointTexture = value; }
        }

        Vector2 _position;

        public Vector2 PointPositon
        {
            get { return _position; }
            set { _position = value; }
        }

        Vector2 cam_position;

        public Vector2 CamPositon
        {
            get { return cam_position; }
            set { cam_position = value; }
        }

        float _pointScale;

        public float PointScale
        {
            get { return _pointScale; }
            set { _pointScale = value; }
        }

        private Rectangle _bound_rect;

        private bool allow_drawing = true;

        public bool AllowDrawing
        {
            get { return allow_drawing; }
            set { allow_drawing = value; }
        }

        public WayPoint(SpriteBatch spriteBatch, Vector2 position, float scale, float h, float l, float t, float vx, float vy) 
            : base(spriteBatch)
        {
            _position = position;
            _pointScale = scale;
            _h = h;
            _l = l;
            _t = t;
            _vx = vx;
            _vy = vy;
            _vt = new CVectorForce(spriteBatch);
            _vtx = new CVectorForce(spriteBatch);
            _vty = new CVectorForce(spriteBatch);

        }

        public override void Init(Microsoft.Xna.Framework.Content.ContentManager contentManager)
        {
            _pointTexture = contentManager.Load<Texture2D>("Images/Launch/ball");

            _bound_rect = new Rectangle();
            _bound_rect.X = (int)(_position.X - _pointTexture.Width/2);
            _bound_rect.Y = (int)(_position.Y - _pointTexture.Height/2);
            _bound_rect.Width = _pointTexture.Width;
            _bound_rect.Height = _pointTexture.Height;


            _vt.Init(contentManager);
            _vt.Create(_position, new Vector2(2*_vx, 2*_vy), 2, Color.Red);

            _vtx.Init(contentManager);
            _vtx.Create(_position, new Vector2(2*_vx, 0), 2, Color.Blue);

            _vty.Init(contentManager);
            _vty.Create(_position, new Vector2(0, 2*_vy), 2, Color.Green);
            base.Init(contentManager);
        }

        public bool HandleInput(Vector2 input_position)
        {
            
            if (IsPointInRectangle(input_position, _bound_rect))
            {
                //if (allow_drawing)
                //{
                //    return true;
                //}
                return true;
            }
            return false;
        }
        private bool IsPointInRectangle(Vector2 point, Rectangle rect)
        {
            if (point.X >= rect.Left && point.X <= rect.Right && point.Y >= rect.Top && point.Y <= rect.Bottom)
            {
                return true;
            }
            return false;
        }

        public override void Update(GameTimerEventArgs timer)
        {
            _vt.Position = _position - (cam_position - new Vector2(240, 400));
            _vtx.Position = _position - (cam_position - new Vector2(240, 400));
            _vty.Position = _position - (cam_position - new Vector2(240, 400));
            base.Update(timer);
        }

        public override void Draw(GameTimerEventArgs timer)
        {
            if (_active)
            {
                _vt.Draw(timer);
                _vtx.Draw(timer);
                _vty.Draw(timer);
            }
            if (allow_drawing)
            {
                SpriteBatch.Draw(_pointTexture, _position, null, Color.White, 0, new Vector2(_pointTexture.Width / 2, _pointTexture.Height / 2), _pointScale, SpriteEffects.None, 0);
            }
            else
            {
                SpriteBatch.Draw(_pointTexture, _position, null, Color.White * 0.3f, 0, new Vector2(_pointTexture.Width / 2, _pointTexture.Height / 2), _pointScale, SpriteEffects.None, 0);
            }
            base.Draw(timer);
        }
    }
}
